package org.neuedu.ssmp.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.neuedu.ssmp.crm.entity.*;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.mapper.*;
import org.neuedu.ssmp.crm.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 客户实现类
 * </p>
 *
 * @author MoonHolder
 * @since 2019-12-09
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public IPage<Customer> conditionPage(int pageIndex, int pageSize, String name, String managerName) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>()
                .like(Objects.nonNull(name), "name", name)
                .like(Objects.nonNull(managerName), "manager_name", managerName);
        return baseMapper.conditionPage(new Page<>(pageIndex, pageSize), queryWrapper);
    }

    @Override
    public List<SysUser> managerList() {
        return new LambdaQueryChainWrapper<>(userMapper).eq(SysUser::getRoleId, 3).list();
    }

    @Override
    public ResponseEntity editCustomerInfo(Customer customer) {
        if (baseMapper.updateById(customer) != 0){
            return ResponseEntity.ok("保存成功！");
        }
        return ResponseEntity.error("保存失败！");
    }

    @Override
    public ResponseEntity inserts(String name, String code, String username, Integer id) {
        baseMapper.inserts(name, code, id, username);
        return ResponseEntity.ok("成功");
    }
}
